<?php

namespace Common\Model;

/**
 * 支付平台支付信息记录
 * @modify trainyao 2016-05-16 创建
 * @index isdelete_type_hotel(`isdelete`, `type`, `hotelentity_id`)
 */
Class VipCardPayInfoModel extends BaseModel {

    protected $trueTableName = 'tb_vip_card_pay_info';
    protected $field = 'id,type,hotelentity_id,vipuser_id,paymethod,tradenumber,outtradenumber,buyer,buyerid,payfee,paytime,paybank,issubscribe,tradetype,more,createtime,updatetime,operator,ischeck,isdelete';

    protected function _before_insert(&$data, $options) {
        $data['operator'] = 0;
    }

    /**
     * 根据订单号与支付方式获取数据
     * @modify wenhui 2018-01-22    创建
     * @param mixin $orderNumber  订单号
     * @param integer $method   支付方式
     * @return array   信息数组,找不到返回空数组
     */
    public function getInfoByOrderNumberMethod($orderNumber, $method = null) {
        $option = array(
            'outtradenumber' => $orderNumber,
            'isdelete' => NOT_DELETE,
        );
        if (!is_null($method)) {
            $option['paymethod'] = $method;
        }

        $data = $this->where($option)->field($this->field)->find();
        return empty($data) ? array() : $data;
    }

    /**
     * 建立支付记录
     * @modify wenhui 2018-01-22    创建
     * @param  integer   $hotelId    客栈ID
     * @param  integer   $vipUserId    会员ID
     * @param  integer   $method         支付方式
     * @param  string   $tradeNumber    第三方平台订单号
     * @param  string   $outTradeNumber 美住订单号
     * @param  string   $buyer          买家
     * @param  string   $buyerId        买家第三方平台ID
     * @param  string   $payfee         支付金额
     * @param  string   $paytime        支付时间
     * @param  string   $paybank        支付银行
     * @param  integer   $isSubscribe    是否关注微信公众号,扩展使用
     * @param  string   $tradeType      支付类型,扩展使用
     * @param  string   $more           更多信息,200字内
     * @param  integer   $type           支付记录类型 0=畅联代收 1=商家自收
     */
    public function addInfo($hotelId, $vipUserId, $method, $tradeNumber = null, $outTradeNumber = null, $buyer = null, $buyerId = null, $payfee = null, $paytime = null, $paybank = null, $isSubscribe = null, $tradeType = null, $more = null, $type = 1) {
        $data = array(
            'hotelentity_id' => $hotelId,
            'vipuser_id' => $vipUserId,
            'paymethod' => $method,
            'type' => $type,
        );
        if (!is_null($tradeNumber)) {
            $data['tradenumber'] = $tradeNumber;
        }
        if (!is_null($outTradeNumber)) {
            $data['outtradenumber'] = $outTradeNumber;
        }
        if (!is_null($buyer)) {
            $data['buyer'] = $buyer;
        }
        if (!is_null($buyerId)) {
            $data['buyerid'] = $buyerId;
        }
        if (!is_null($payfee)) {
            $data['payfee'] = $payfee;
        }
        if (!is_null($paytime)) {
            $data['paytime'] = $paytime;
        }
        if (!is_null($paybank)) {
            $data['paybank'] = $paybank;
        }
        if (!is_null($isSubscribe)) {
            $data['issubscribe'] = $isSubscribe;
        }
        if (!is_null($tradeType)) {
            $data['tradetype'] = $tradeType;
        }
        if (!is_null($more)) {
            $data['more'] = $more;
        }

        return empty($this->add($data)) ? false : true;
    }
}
